package com.zhuiyun.educations.api.order.mapper;

import com.zhuiyun.educations.api.order.entity.DTO.OrderEntityDTO;
import com.zhuiyun.educations.common.mapper.BaseMapper;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.*;
import com.zhuiyun.educations.api.order.entity.Order;
import com.zhuiyun.educations.util.pageUtil.PageRequest;

import java.util.List;

/**
 * @ClassName OrderMapper
 * @Description TODO 订单管理
 * @Author ys
 * @Date 2023/6/19 19:28
 **/
@Mapper
public interface OrderMapper extends BaseMapper<Order> {
    /**
     * @Author ys
     * @Description TODO 分页查询
     * @Date 2023/6/26 14:09
     * @Return CommonResult
     * @param
     */
    @Select("SELECT *,TIMESTAMPDIFF(SECOND , now(), o.close_time) as residue_time FROM `order` o  LEFT JOIN course_info c ON o.course_id = c.id LEFT JOIN user u on o.user_id = u.id where o.del_flag=0")
    List<Order> getFilePrice(PageRequest pageRequest);
    @Select("SELECT *,TIMESTAMPDIFF(SECOND , now(), o.close_time) as residue_time FROM `order` o  LEFT JOIN course_info c ON o.course_id = c.id LEFT JOIN user u on o.user_id = u.id where o.del_flag=0 and o.course_id=#{courseId} and o.user_id=#{userId} and order_state='0'")
    Order getFilePriceById(@Param("courseId") Integer courseId,@Param("userId")Integer userId);
    @Select("SELECT *,TIMESTAMPDIFF(SECOND , now(), o.close_time) as residue_time FROM `order` o  LEFT JOIN course_info c ON o.course_id = c.id LEFT JOIN user u on o.user_id = u.id where o.del_flag=0 and o.course_id=#{courseId} and o.user_id=#{userId} and order_state='1'")
    Order getorderById(@Param("courseId") Integer courseId,@Param("userId")Integer userId);
    /**
    * @Author cjl
    * @Description TODO 前端查询用户订单
    * @Date 2023/7/5 17:26
    * @Return java.util.List<com.zhuiyun.educations.api.order.entity.DTO.OrderEntityDTO>
    * @param userId
    * @param orderState
    */
    @Select("<script>" +
            "SELECT\n" +
            "\t`order`.*,\n" +
            "\tcourse_info.course_title,\n" +
            "\tcourse_info.image_id,\n" +
            "\tcourse_info.pay_type," +
            "\tprice.price \n" +
            "FROM\n" +
            "\t`order`\n" +
            "\tLEFT JOIN course_info ON course_info.id = `order`.course_id\n" +
            "\tLEFT JOIN price ON course_info.id = price.course_id \n" +
            "WHERE\n" +
            "  `order`.user_id = #{userId}\n" +
            "\tAND course_info.pay_type = 0\n" +
            "<if test='orderState!=null'>" +
            "\tAND `order`.order_state = #{orderState}\n" +
            "</if>"+
            "\tAND `order`.del_flag = 0<" +
            "/script>")
    List<OrderEntityDTO> selectOrderEntityDTO(Integer userId, Integer orderState);
    @Insert("insert into `order` (user_id,order_state,order_number,payment_type,pay_price,del_flag,course_id,create_time,close_time,update_time) values (#{userId},#{orderState},#{orderNumber},#{paymentType},#{payPrice},#{delFlag},#{courseId},#{createTime},#{closeTime},#{updateTime})")
    int add(Order order);
    @Update("update `order` set del_flag=1,update_time=now() where id=#{id}")
    int update(Integer id);
    @Update("update `order` set order_state=1,update_time=now() where course_id=#{id}")
    int updatestate(Integer id);
}
